﻿
function PreviewImage(imgFile) {
    var filextension = imgFile.value.substring(imgFile.value.lastIndexOf("."), imgFile.value.length);
    filextension = filextension.toLowerCase();
    if ((filextension != '.jpg') && (filextension != '.gif') && (filextension != '.jpeg') && (filextension != '.png') && (filextension != '.bmp')) {
        alert("对不起，系统仅支持标准格式的照片，请您确认格式后重新上传，谢谢 !");
        imgFile.focus();
    }
    else {
        var path;
        if (document.all)//IE 
        {
            if (imgFile) {
                path = imgFile.value;
            } else {path = "";}
            // path = "/UpLoadFile/Design/北京/翠微店(PAWH00002).jpg";
            document.getElementById("imgPreview").innerHTML = "";
            document.getElementById("imgPreview").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='scale',src=\"" + path + "\")"; //使用滤镜效果 
        }
        else//FF 
        {
            if (imgFile) {
                path = imgFile.files[0].getAsDataURL();
            } else {
                path = "";
            }
            document.getElementById("imgPreview").innerHTML = "<img id='img1' width='120px' height='100px' src='" + path + "'/>";
            //document.getElementById("img1").src = path; 
        }
        document.getElementById("imgPreview").style.display = "";
        document.getElementById("imgView").style.display = "none";
    }
} 
function AddImg() {
    var Container = $("#divHide");
    var newfilenum = Math.random().toString().replace(".", "");
    var f1 = $("<input type='file' name='fileNewImg' id='fileNewImg' newfilenum='" + newfilenum + "' onchange=FileCapture(this,'" + newfilenum + "') style='display:block;' />");
    Container.append(f1);
    //f1.click();
}
function FileCapture(file1,newFilenum) {
    var dHdf = $("#hdfImgData");
    var tagUL = $("#imgTagList");
    if (file1.value) {
        //将json反序列化为对象
        var arr = eval(dHdf.val());
        //暂存数据
        var maxIdx = getMaxIdx(arr);
        var obj = CreateImgObj(maxIdx+1, file1.value, 2, "", newFilenum);
        arr.push(obj);
        //反序列化图片的页签列表
        DeserailImgtagsarea(tagUL, arr, arr.length - 1);
        //加载当前图
        LoadCurImg(obj);
        //序列化数据--JsonSerial.js
        dHdf.val(Serialize(arr));
        //alert(dHdf.val());
    } else {
        $(file1).remove();//file无值，则取消
    }
}
function getMaxIdx(arr) {//debugger;
    var tmpIdx = -1;
    for(var i=0;i<arr.length;i++){
        if(parseInt(arr[i].idx)>tmpIdx){
            tmpIdx = parseInt(arr[i].idx);
        }
    }
    return tmpIdx;
}
function DeserailImgtagsarea(tagUL,dataArr, curIdx) {
    $("li", tagUL).each(function () { $(this).remove(); });
    var n=dataArr.length;
    for (var i = 0; i < n; i++) {
        if (dataArr[i].status == 1 || dataArr[i].status == 2){
            var stylecls;
            var changeImg;
            if (i === curIdx) {
                stylecls = "curIdxTag";
            } else {
                stylecls = "otherIdxTag";
            }
            tagUL.append("<li><a class='" + stylecls + "' onclick='ChangeViewImg(this);' imgidx ='" + dataArr[i].idx + "'>图" + (i + 1) + "</a></li>");
         }
    }
}
function ChangeViewImg(obj) {//debugger;
//    if ($(obj).attr("class") === "otherIdxTag") {
        var i = $(obj).attr("imgidx");
        var dHdf = $("#hdfImgData");
        var dataArr = eval(dHdf.val());
        var tmpData = null;
        for (var j = 0; j < dataArr.length; j++) {
            if (dataArr[j].idx == i) {
                tmpData = dataArr[j];
            }
        }
        LoadCurImg(tmpData);
        $("a[class=curIdxTag]", $(obj).parent().parent()).attr("class", "otherIdxTag"); //置为“普通”
        $(obj).attr("class", "curIdxTag");        
//    } 
}
function LoadCurImg(curData) {
    var svrImg = $("#imgView");
    var locImg = $("#imgPreview");
    var Container = $("#divHide");
    if (curData.status == 2) {
        var file = $("input[type=file][newfilenum='" + curData.newfilenum + "']", Container);
        if (file.length > 0) {
            PreviewImage(file[0]);
        }
    }
    else if (curData.status == 1) {
        svrImg.attr("src", curData.path+"?rdm="+Math.random());
        svrImg.show();
        locImg.hide();
    } else if (curData.status == 3) {
    }
}
function DelImg() {
    var tagUL = $("#imgTagList");
    var curTag = $("a[class=curIdxTag]",  $("#imgTagList"));
    if (curTag.length > 0 && confirm("确认删除" + curTag.text() + "么？")) {
        //debugger;
        //删除数据
        var dHdf = $("#hdfImgData");
        var dataArr = eval(dHdf.val());
        var L =dataArr.length;
        for (var i = L-1; i >=0 ; i--) {
            if (dataArr[i].idx == curTag.attr("imgidx")) {
                if (dataArr[i].status == "2") {
                    var newfnum = dataArr[i].newfilenum;
                    $("input[newfilenum='" + newfnum + "']").remove(); //移除file                    
                    dataArr.splice(i, 1); //移除数据
                }else if(dataArr[i].status == "1"){
                    dataArr[i].status = "3";//打上删除标记
                }
                //反序列化图片的页签列表
                DeserailImgtagsarea(tagUL,dataArr,-1);
                //加载当前图
                //LoadCurImg(obj);
                break;
            }
        }
        var atag = $("a", $("#imgTagList"));
        if (atag.length > 0) {//debugger;
            ChangeViewImg(atag[0]); //切换到第1图
        } else {
            $("#imgPreview").hide();
            $("#imgView").attr("src","/inc/images/space.gif").show();
        }
        $("#hdfImgData").val(Serialize(dataArr));
    }
}
function ImgInit() {
    var dHdf = $("#hdfImgData");
    var dataArr = eval(dHdf.val());
    var tagUL = $("#imgTagList");
    if(dataArr.length>0)
    {
        //反序列化图片的页签列表
        DeserailImgtagsarea(tagUL, dataArr, 0);
        ChangeViewImg($("a", tagUL)[0]);
     }
}
/*
Idx(序号)
Status（1：已保存服务器的 2：新增的Img需要保存到服务器  3:要从服务器删除的）
Path(属状态1,3服务器上img的，显示网站上图片路径；属状态2的，显示本地浏览路径)
*/
function CreateImgObj(Idx,Path,Status,Memo,NewFilenum) {
    var obj = {};
    obj.idx = Idx;
    obj.path = Path;
    obj.status = Status;
    obj.memo = Memo;
    obj.newfilenum = NewFilenum;
    return obj;
}
/*
    说明：调用ajax删除表格上的选中行的文件和数据

*/
 function DeleteFilepack() {
            var tbl = $("#tblFilepack");
            var chxs = $("input:checkbox:checked", tbl);
            var strIds = "0";
            if (chxs.length == 0) {
                return;
            }
            if (chxs.length > 0) {
                if(!confirm("您确认要删除选中的" + chxs.length + "项么？")){
                   return; 
                }
            }
            for (var i = 0; i < chxs.length; i++) {
                strIds = strIds + "|"+ chxs[i].value;
            }
            $.ajax({
                type: "POST",
                dataType: "json",
                async: false, //ajax的请求时同步 只有一个线程
                url: "/design/FilepackDel.ashx",
                data: "filepackids=" + strIds ,
                success: function (obj) {
                    if(obj.status.toLowerCase()=="true"){

                        //删除表格上的行
                         for (var i = 0; i < chxs.length; i++) {
                                $(chxs[i]).parent().parent().remove();
                            }
                        alert(obj.msg);

                    }else{
                        alert(obj.msg);
                    }
                }
            });
   }